﻿///**四.用到一些 方法  ***********************///
//4.1获取某月的最后一天
function getFirstAndLastMonthDay(year, month) {
	var firstdate = year + '-' + month + '-01';
	var day = new Date(year, month, 0);
	var lastdate = year + '-' + month + '-' + day.getDate(); //获取当月最后一天日期    
	//给文本控件赋值。同下  
	return lastdate;
}
//4.2判断是否是闰年
function isLeapYear(year) {
	return(year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
//判断每个月的天数
function getMonthDays(year, month) {
	return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
}
//判断所选日期是第几周
function getWeekNumber(obj) {
	year = obj.getFullYear(),
		month = obj.getMonth(),
		days = obj.getDate();
	for(var i = 0; i < month; i++) {
		days += getMonthDays(year, i);
	}
	var yearFirstDay = new Date(year, 0, 1).getDay() || 7;
	var week = null;
	if(yearFirstDay == 1) {
		week = Math.ceil(days / yearFirstDay);
	} else {
		days -= (7 - yearFirstDay + 1);
		week = Math.ceil(days / 7) + 1;
	}
	return week;
}

///***************** 两个图表数据  ***********************///
function loadHuanBi(a) {

	var x = []; //X轴
	var y = []; //Y轴
	var xtext = []; //X轴TEXT
	var colorOne = ["#4f81bd", "#4f81bd", "#4f81bd", "#4f81bd", "#4f81bd", "#4f81bd", "#4f81bd", "#4f81bd", "#4f81bd"];
	var color = ["#c0504d", "#c0504d", "#c0504d", "#c0504d", "#c0504d", "#c0504d", "#c0504d", "#c0504d", "#c0504d"];
	$.ajax({
		type: 'post',
		url: 'http://47.92.116.158:8099/BhGgjAPI/ChartData.asmx/' + a.getLink + '?callback=?', //请求数据的地址
		data: {
			"year1": a.getCurrentYear,
			"week1": a.get_currentMonth_lastWeek,
			"year2": a.getCurrentYear,
			"week2": a.get_currentMonth_firstWeek,
			"year_last1": a.get_lastMonth_Year,
			"week_last1": a.get_lastMonth_lastWeek,
			"year_last2": a.get_lastMonth_Year,
			"week_last2": a.get_lastMonth_firstWeek
		},
		dataType: "json",
		success: function(data) {
			//var json = eval("(" + data + ")");
			var json = data;
			//console.log(json);
			var s = 1;
			//出让
			for(var key in json.currentMonth) {
				json.currentMonth[key].y = parseInt(json.currentMonth[key].NUM); //给Y轴赋值
				xtext[key] = json.currentMonth[key].TITLE; //给X轴TEXT赋值
				json.currentMonth[key].color = colorOne[key];
			}
			chart.series[0].setData(json.currentMonth); //数据填充到highcharts上面

			//划拨
			for(var key in json.lastMonth) {
				json.lastMonth[key].y = parseInt(json.lastMonth[key].NUM); //给Y轴赋值
				xtext[key] = json.lastMonth[key].TITLE; //给X轴TEXT赋值     
				json.lastMonth[key].color = color[key];
			}
			chart.series[1].setData(json.lastMonth); //数据填充到highcharts上面
		},
		error: function(e) {

		}
	});
	var chart = new Highcharts.Chart({
		//color: ["#d7d7d7", "#3992fd", "#f9873a", "#cc33cc"],
		chart: {
			renderTo: 'HuanBiChart',
			type: 'column' //显示类型 柱形
		},
		title: {
			text: '' //图表的标题
		},
		//当鼠标经过时的提示设置
		tooltip: {
			headerFormat: '<span style="font-size:11px"><b>{series.name}<b></span><br>',
			pointFormat: '<span style="color:{point.color}">{point.TITLE}</span>：<b>{point.y}</b><br/>'
		},
		xAxis: {
			categories: xtext
		},
		yAxis: {
			title: {
				text: '', //Y轴的名称
				style: {
					color: '#333333',
					fontWeight: 'bold',
					fontSize: '14px'
				}
			}
		},
		series: [{
			name: "本月（" + a.getCurrentMonth + "月）"
		}, { name: "上月（" + a.get_lastMonth + "月）" }],
		plotOptions: { //plotOptions全局设置
			series: { //对series的全局设置                        
				cursor: 'pointer',
				dataLabels: {
					enabled: true, //是否在节点上显示数据
					color: '#333',
					align: 'center',
					style: { fontSize: '13px' }
				}
			}
		},
		credits: {
			enabled: false //隐藏右下角网址
		},
		exporting: {
			enabled: false //隐藏右上角打印
		}
	});
};

//loadHuanBi("getProjectArcLinkChart");

function loadTongBi(a) {
	//var url = "";
	//if (str)
	//{

	//}
	var x = []; //X轴
	var y = []; //Y轴
	var xtext = []; //X轴TEXT
	//var color = ["#2aac20"];   
	$.ajax({
		type: 'get',
		url: 'http://47.92.116.158:8099/BhGgjAPI/ChartData.asmx/' + a.getYear + '?callback=?', //请求数据的地址
		data: {
			"year1": a.getCurrentYear,
			"week1": a.get_currentMonth_lastWeek,
			"year2": a.getCurrentYear,
			"week2": a.get_currentMonth_firstWeek,
			"year_last1": a.get_lastYear,
			"week_last1": a.get_lastYear_lastWeek,
			"year_last2": a.get_lastYear,
			"week_last2": a.get_lastYear_firstWeek
		},
		dataType: "json",
		success: function(data) {
			//var json = eval("(" + data + ")");
		    var json = data;
            
            console.log(data)
		    ///** 这里是处理两组数组进行百分比的 算法 start ******************************************//
		    var get_currentYear_numList = "";  //定义变量存放今年的数量
		    $.each(json.currentYear, function (index, item) {
		        get_currentYear_numList += item.NUM + ",";
		    });
		    get_currentYear_numList = get_currentYear_numList.substring(0, get_currentYear_numList.length - 1);
		    var arry_currentYear_numList = get_currentYear_numList.split(",");//声明数组
		    //console.log(arry_currentYear_numList);
		    var get_lastYear_numList = "";//定义变量存放上年的数量
		    $.each(json.lastYear, function (index, item) {
		        get_lastYear_numList += item.NUM + ",";
		    });
		    get_lastYear_numList = get_lastYear_numList.substring(0, get_lastYear_numList.length - 1);
		    var arry_lastYear_numList = get_lastYear_numList.split(","); //声明数组
		    //console.log(arry_lastYear_numList);
		    
		    var arry_perList = [];//声明数组		   
		    var tipList = "";
		    for (var i = 0; i < arry_currentYear_numList.length; i++) {		        
		        var getDivision = arry_currentYear_numList[i] / arry_lastYear_numList[i];
		        var setTip = "增加";
		        if (getDivision > 1) {
		            getDivision = getDivision - 1;		            
		            getDivision = (getDivision * 100).toFixed(1);
		            setTip = "增加";
		        } else {
		            getDivision = 1 - getDivision;
		            getDivision = (getDivision * 100).toFixed(1);
		            setTip = "减少";
		        }	
		        arry_perList.push(getDivision);
		        tipList += setTip + ",";		        
		    }	
		    //console.log(arry_perList);		    
		    tipList = tipList.substring(0, tipList.length - 1);
		    var arry_tipList = tipList.split(","); //声明数组
		    //console.log(arry_tipList);
		    var curt_currentYear = "\"" + a.getCurrentYear + "\""; //截取今年后两位
		    curt_currentYear = curt_currentYear.substring(3, 5);
		    var curt_lastYear = "\"" + a.get_lastYear + "\""; //截取上一年后两位
		    curt_lastYear = curt_lastYear.substring(3, 5);
		    ///** 这里是处理两组数组进行百分比的 算法 end ******************************************//
		    
			var s = 1;
			//今年
			for (var key in json.currentYear) {
			    json.currentYear[key].curt_currentYear = curt_currentYear;//截取 今年
			    json.currentYear[key].curt_lastYear = curt_lastYear;  //截取 上年
			    json.currentYear[key].per = arry_perList[key]; //将 百分比 赋给 折线图
			    json.currentYear[key].tip = arry_tipList[key]; //将 提示语 赋给 折线图
				json.currentYear[key].y = parseInt(json.currentYear[key].NUM); //给Y轴赋值
				xtext[key] = json.currentYear[key].TITLE; //给X轴TEXT赋值
				//json.toSell[key].color = color[key];
			}
			chart.series[0].setData(json.currentYear); //数据填充到highcharts上面

			//上年
			for (var key in json.lastYear) {
			    json.lastYear[key].curt_currentYear = curt_currentYear;//截取 今年
			    json.lastYear[key].curt_lastYear = curt_lastYear;  //截取 上年
			    json.lastYear[key].per = arry_perList[key]; //将 百分比 赋给 折线图
			    json.lastYear[key].tip = arry_tipList[key]; //将 提示语 赋给 折线图
				json.lastYear[key].y = parseInt(json.lastYear[key].NUM); //给Y轴赋值
				xtext[key] = json.lastYear[key].TITLE; //给X轴TEXT赋值                        
			}
			chart.series[1].setData(json.lastYear); //数据填充到highcharts上面
		},
		error: function(e) {

		}
	});
	var chart = new Highcharts.Chart({
		colors: ['#2aac20', '#fa4a54'], //绘图线颜色
		chart: {
			renderTo: 'TongBiLine',
			type: 'line' //显示类型 折线
		},
		title: {
			text: '' //图表的标题
		},
		//当鼠标经过时的提示设置
		tooltip: {
			headerFormat: '<span style="font-size:11px"><b>{series.name}<b></span><br>',
			pointFormat: '<span style="color:{point.color}">{point.TITLE}</span>：{point.y}<br/><span style="color:blue;">{point.curt_currentYear}年比{point.curt_lastYear}年：</span>{point.tip}{point.per}%<br/>'
		},
		xAxis: {
			categories: xtext
		},
		yAxis: {
			title: {
				text: '', //Y轴的名称
				style: {
					color: '#333333',
					fontWeight: 'bold',
					fontSize: '14px'
				}
			}
		},
		series: [{
			name: a.getCurrentYear + "（" + a.getCurrentMonth + "月）"
			//data: [7.0, 6.9, 9.5, 14.5, 18.4]
		}, {
			name: a.get_lastYear + "（" + a.getCurrentMonth + "月）"
			//data: [3.9, 4.2, 5.7, 8.5, 11.9]
		}],
		plotOptions: { //plotOptions全局设置
			line: {
				dataLabels: {
					enabled: true // 开启数据标签
				},
			},
			series: { //对series的全局设置                        
				cursor: 'pointer',
				marker: {
					symbol: 'diamond' //节点的形状曲线点类型："circle", "square", "diamond", "triangle","triangle-down"，默认是"circle"
				},
				dataLabels: {
					enabled: true, //是否在节点上显示数据
					color: '#333',
					align: 'center',
					style: { fontSize: '13px' }
				}
			}
		},
		credits: {
			enabled: false //隐藏右下角网址
		},
		exporting: {
			enabled: false //隐藏右上角打印
		}
	});

}


$(".statisticsChart-tab span").each(function() {
	$(this).click(function() {
		$(this).addClass("tabActive").siblings().removeClass("tabActive");
	});
});

$(function  () {
	$(".statisticsChart-tab span").eq(0).click()
})



var chartVue = new Vue({
	el: "body",
	data: {
		strApi: "", //接口类型
        
		getCurrentYear: "", //环比
		get_currentMonth_lastWeek: "",
		get_currentMonth_firstWeek: "",
		get_lastMonth_Year: "",
		
		get_lastMonth_lastWeek: "",
		get_lastMonth_firstWeek: "",
			
		get_lastYear:"",        //同比
		get_lastYear_lastWeek:"",
	    get_lastYear_firstWeek:"",
	    
	    getLink:"",        //
	    getYear:"",       //
	    
	    getCurrentMonth:"",
	    get_lastMonth:""
 
	},
	methods: {
		projectChart:function  () {
			_this.getLink="getProjectArcLinkChart"
			_this.getYear="getProjectArcYearChart"
			loadTongBi(_this)
			
			loadHuanBi(_this)
			
		}
	},
	created: function() {
		_this=this
		
		var objDate = new Date();
		_this.getCurrentYear = objDate.getYear();
		if(_this.getCurrentYear < 1900) _this.getCurrentYear = _this.getCurrentYear + 1900;
		_this.getCurrentMonth = objDate.getMonth() + 1;
		//alert(getCurrentMonth);

		//一.判断当年月的参数
		var get_currentMonth_firstDay = (_this.getCurrentYear + "/" + _this.getCurrentMonth + "/1 00:00:00");
		//console.log(getWeekNumber(new Date(get_currentMonth_firstDay)))
		_this.get_currentMonth_firstWeek = getWeekNumber(new Date(get_currentMonth_firstDay)); //这月的1号是第几周
		var get_currentMonth_firstDay_weekday = new Date(get_currentMonth_firstDay).getDay(); //今天是周几
		
		if(get_currentMonth_firstDay_weekday == "6" || get_currentMonth_firstDay_weekday == "0") {
			_this.get_currentMonth_firstWeek = _this.get_currentMonth_firstWeek + 1; //如果第一天 大于星期5， 则周数自加1
		}
		var get_currentMonth_lastDay = getFirstAndLastMonthDay(_this.getCurrentYear, _this.getCurrentMonth).replace(/\-/g, "/"); //这月的最后一天
		_this.get_currentMonth_lastWeek = getWeekNumber(new Date(get_currentMonth_lastDay));; //上月最后一周
		
		var get_currentMonth_lastDay_weekDay = new Date(get_currentMonth_lastDay).getDay(); //获取上月 最后一天 星期几
		if(get_currentMonth_lastDay_weekDay < 5 && get_currentMonth_lastDay_weekDay > 0) {
			_this.get_currentMonth_lastWeek = _this.get_currentMonth_lastWeek - 1; //如果最后一天小于周五，周数减一
		}

		//alert(get_currentMonth_lastWeek);

		///**二.获取上月的参数  ***********************///
		if(_this.getCurrentMonth == "1") {
			_this.get_lastMonth = 12;
			_this.get_lastMonth_Year = parseInt(_this.getCurrentYear - 1);
			var get_lastMonth_firstDay = (_this.get_lastMonth_Year + "/" + _this.get_lastMonth + "/1 00:00:00") //上月的第一天
			//console.log(get_lastMonth_firstDay)
			_this.get_lastMonth_firstWeek = getWeekNumber(new Date(get_lastMonth_firstDay));; //上月第一周
			var get_lastMonth_firstDay_weekDay = objDate.getDay(); //获取上月 第一天 星期几
			if(get_lastMonth_firstDay_weekDay == "6" || get_lastMonth_firstDay_weekDay == "0") {
				_this.get_lastMonth_firstWeek = _this.get_lastMonth_firstWeek + 1; //如果第一天 大于星期5， 则周数自加1
			}

			var get_lastMonth_lastDay = (_this.get_lastMonth_Year + "/" + _this.get_lastMonth + "/31 00:00:00"); //上月的最后一天
			//console.log(get_lastMonth_lastDay)
			_this.get_lastMonth_lastWeek = getWeekNumber(new Date(get_lastMonth_lastDay));; //上月最后一周
			var get_lastMonth_lastDay_weekDay = objDate.getDay(); //获取上月 最后一天 星期几

			if(get_lastMonth_lastDay_weekDay < 5 && get_lastMonth_lastDay_weekDay > 0) {
				_this.get_lastMonth_lastWeek = _this.get_lastMonth_lastWeek - 1; //如果最后一天小于周五，周数减一
			}
		} else {
			_this.get_lastMonth = parseInt(_this.getCurrentMonth - 1); //相减得到 上月
			var get_lastMonth_firstDay = (_this.getCurrentYear + "/" + _this.get_lastMonth + "/1 00:00:00"); //上月的第一天
            //console.log(get_lastMonth_firstDay)
			_this.get_lastMonth_firstWeek = getWeekNumber(new Date(get_lastMonth_firstDay));; //上月第一周
			_this.get_lastMonth_Year = new Date(get_lastMonth_firstDay).getYear(); //获取上月所在的 年份
			if(_this.get_lastMonth_Year < 1900) { _this.get_lastMonth_Year = _this.get_lastMonth_Year + 1900; }

			var get_lastMonth_firstDay_weekDay = new Date(get_lastMonth_firstDay).getDay();
			if(get_lastMonth_firstDay_weekDay == "6" || get_lastMonth_firstDay_weekDay == "0") {
				_this.get_lastMonth_firstWeek = _this.get_lastMonth_firstWeek + 1; //如果第一天 大于星期5， 则周数自加1
			}
            
			//var get_lastMonth_lastDay = get_lastMonth_Year + "-" + get_lastMonth + "- 31 00:00:00"; //上月的最后一天
			var get_lastMonth_lastDay = getFirstAndLastMonthDay(_this.get_lastMonth_Year, _this.get_lastMonth).replace(/\-/g, "/"); //上月的最后一天

			_this.get_lastMonth_lastWeek = getWeekNumber(new Date(get_lastMonth_lastDay));; //上月最后一周
			var get_lastMonth_lastDay_weekDay = new Date(get_lastMonth_lastDay).getDay(); //获取上月 最后一天 星期几  

			if(get_lastMonth_lastDay_weekDay < 5 && get_lastMonth_lastDay_weekDay > 0) {
				_this.get_lastMonth_lastWeek = _this.get_lastMonth_lastWeek - 1; //如果最后一天小于周五，周数减一
			}
			//alert(get_lastMonth_lastWeek); //打印上月最后一周
		}

		///**三.获取上一年的 同比 本月 的参数  ***********************///
		_this.get_lastYear = parseInt(_this.getCurrentYear - 1);
		var get_lastYear_Month = _this.getCurrentMonth; //上年本月
		var get_lastYear_firstDay = (_this.get_lastYear + "/" + get_lastYear_Month + "/1 00:00:00"); //上年同比月的第一天
		_this.get_lastYear_firstWeek = getWeekNumber(new Date(get_lastYear_firstDay));; //上年同比月第一周
		var get_lastYear_Year = new Date(get_lastYear_firstDay).getYear(); //获取上年同比月所在的 年份
		var get_lastYear_firstDay_weekDay = new Date(get_lastYear_firstDay).getDay();
		if(get_lastYear_firstDay_weekDay == "6" || get_lastYear_firstDay_weekDay == "0") {
			_this.get_lastYear_firstWeek = _this.get_lastYear_firstWeek + 1; //如果第一天 大于星期5， 则周数自加1
		}
		var get_lastYear_lastDay = getFirstAndLastMonthDay(_this.get_lastYear, get_lastYear_Month).replace(/\-/g, "/"); //上年同比月的最后一天
		_this.get_lastYear_lastWeek = getWeekNumber(new Date(get_lastMonth_lastDay));; //上年同比月最后一周
		var get_lastYear_lastDay_weekDay = new Date(get_lastMonth_lastDay).getDay(); //获取上年同比月的 最后一天 星期几
		if(get_lastYear_lastDay_weekDay < 5 && get_lastYear_lastDay_weekDay > 0) {
			_this.get_lastYear_lastWeek = _this.get_lastYear_lastWeek - 1; //如果最后一天小于周五，周数减一
		}
		
	}
})